package com.sky.Sdk;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;

import java.util.concurrent.TimeUnit;

/**
 * Created by sun on 2017-06-16.
 */
public class ClientConnectListener implements ChannelFutureListener {
    private ClientBootstrap clientBootstrap;
    /**
     * 重连间隔时间
     */
    private int reconnectInterv;

    ClientConnectListener(ClientBootstrap clientBootstrap) {
        this.clientBootstrap = clientBootstrap;
        this.reconnectInterv = 1;
    }

    ClientConnectListener(ClientBootstrap clientBootstrap,int reconnectInterv) {
        this.clientBootstrap = clientBootstrap;
        this.reconnectInterv = reconnectInterv;
    }

    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        if(!channelFuture.isSuccess()) {
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule(new Runnable() {
                public void run() {
                    try {
                        clientBootstrap.connect();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },reconnectInterv, TimeUnit.SECONDS);
        }
    }
}
